﻿using BussinessObject.ObjectTwo;
using Class_Entity.ObjectTwo;
using ProjectHK3.App_Code;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectHK3.ObjectTwo
{
    public partial class NewOrder : System.Web.UI.Page
    {
        B_LoadDrop ex = new B_LoadDrop();
        DataTable tb = new DataTable();
        B_Insert fx = new B_Insert();
        Orders sx = new Orders();
        B_Cal kx = new B_Cal();
        C_Calculator gx = new C_Calculator();
        protected void Page_Load(object sender, EventArgs e)
        {            
       
            if (!IsPostBack)
            {
                Panel2.Visible = false;
                loadData();
                
            }
        }
        public void loadData()
        {        
            DropDownList1.DataSource = ex.city();
            DropDownList1.DataTextField = "CITY_NAME";
            DropDownList1.DataValueField = "CITY_ID";
            DropDownList1.DataBind();

            DropDownList2.DataSource = ex.city();
            DropDownList2.DataTextField = "CITY_NAME";
            DropDownList2.DataValueField = "CITY_ID";
            DropDownList2.DataBind();

            DropDownList4.DataSource = ex.service();
            DropDownList4.DataTextField = "SERVICE_NAME";
            DropDownList4.DataValueField = "SERVICE_ID";
            DropDownList4.DataBind();

            DropDownList5.DataSource = ex.Bran();
            DropDownList5.DataTextField = "BRANCH_NAME";
            DropDownList5.DataValueField = "BRANCH_ID";
            DropDownList5.SelectedValue = Session["UserBran"].ToString();
            DropDownList5.DataBind();

            DropDownList6.DataSource = ex.Bran();
            DropDownList6.DataTextField = "BRANCH_NAME";
            DropDownList6.DataValueField = "BRANCH_ID";
            DropDownList6.DataBind();

            DropDownList7.DataSource = ex.service();
            DropDownList7.DataTextField = "SERVICE_NAME";
            DropDownList7.DataValueField = "SERVICE_ID";
            DropDownList7.DataBind();

            DropDownList8.DataSource = ex.Bran();
            DropDownList8.DataTextField = "BRANCH_NAME";
            DropDownList8.DataValueField = "BRANCH_ID";
            DropDownList8.SelectedValue = Session["UserBran"].ToString();
            DropDownList8.DataBind();

            DropDownList9.DataSource = ex.Bran();
            DropDownList9.DataTextField = "BRANCH_NAME";
            DropDownList9.DataValueField = "BRANCH_ID";
            DropDownList9.DataBind();
        }
        public void InsertOderA()
        {
            sx.ORDER_NUMBER = TextBox1.Text;
            sx.ORDER_DATE = DateTime.Now.ToString("dd/MM/yyyy");
            sx.IS_INTERNAL_DELIVERY = 0;
            sx.SENDER_NAME = TextBox2.Text;
            sx.ACCOUNT_ID = int.Parse(Session["Userid"].ToString());
            sx.SENDER_PHONE = TextBox3.Text;
            sx.SENDER_ADDRESS = TextBox4.Text;
            sx.SENDER_CITY = int.Parse(DropDownList1.SelectedValue.ToString());
            sx.RECEIVER_NAME = TextBox5.Text;
            sx.RECEIVER_PHONE = TextBox6.Text;
            sx.RECEIVER_ADDRESS = TextBox7.Text;
            sx.RECEIVER_CITY = int.Parse(DropDownList2.SelectedValue.ToString());
            sx.MAIL_CODE = TextBox8.Text;
            sx.WEIGHT = float.Parse(TextBox9.Text);
            sx.DESCRIPTION = TextBox18.Text;
            sx.SERVICE_ID = int.Parse(DropDownList4.SelectedValue.ToString());
            sx.BRANCH_ID_FROM = int.Parse(DropDownList5.SelectedValue.ToString());
            sx.BRANCH_ID_TO = int.Parse(DropDownList6.SelectedValue.ToString());
            sx.TOTAL_DELIVERY_FEE = float.Parse(TextBox10.Text);
            sx.TOTAL_PAID = float.Parse(TextBox12.Text);
            sx.STATUS = "Sendding";
            if (fx.InsertOrder(sx) == true)
            {
                Alert.Show("Insert Sucessfull!!!");
            }
            else
            {
                Alert.Show("Insert Error!!!");
            }


        }

        public void InsertOderB()
        {
            sx.ORDER_NUMBER = TextBox1.Text;
            sx.ORDER_DATE = DateTime.Now.ToString("dd/MM/yyyy");
            sx.IS_INTERNAL_DELIVERY = 1;           
            sx.ACCOUNT_ID = int.Parse(Session["Userid"].ToString());          
            sx.MAIL_CODE = TextBox14.Text;
            sx.WEIGHT = float.Parse(TextBox15.Text.ToString());
            sx.DESCRIPTION = TextBox19.Text;
            sx.SERVICE_ID = int.Parse(DropDownList7.SelectedValue.ToString());
            sx.BRANCH_ID_FROM = int.Parse(DropDownList8.SelectedValue.ToString());
            sx.BRANCH_ID_TO = int.Parse(DropDownList9.SelectedValue.ToString());
            sx.TOTAL_DELIVERY_FEE = float.Parse(TextBox16.Text);
            sx.TOTAL_PAID = float.Parse(TextBox17.Text);
            sx.STATUS = "Sendding";
            if (fx.InsertOrderB(sx) == true)
            {
                Alert.Show("Insert Sucessfull!!!");
            }
            else
            {
                Alert.Show("Insert Error!!!");
            }


        }
        public void cal()
        {
           gx = kx.Dis(int.Parse(DropDownList5.SelectedValue.ToString()), int.Parse(DropDownList6.SelectedValue.ToString()));
            int vala;
            vala = int.Parse(gx.KC.ToString());
            TextBox10.Text = vala.ToString();
            gx = kx.FEE(vala, int.Parse(DropDownList4.SelectedValue.ToString()));
            float a1, a2, a3;
            a1 = gx.FEE_PER_KG;
            a2 = gx.FEE_PER_KM;
            a3 = (vala * a2) + (a1 * float.Parse(TextBox9.Text.ToString()));
            TextBox11.Text = a3.ToString();
        }
        public void calB()
        {
            gx = kx.Dis(int.Parse(DropDownList8.SelectedValue.ToString()), int.Parse(DropDownList9.SelectedValue.ToString()));
            int vala;
            vala = int.Parse(gx.KC.ToString());
            TextBox16.Text = vala.ToString();
            gx = kx.FEE(vala, int.Parse(DropDownList7.SelectedValue.ToString()));
            float a1, a2, a3;
            a1 = gx.FEE_PER_KG;
            a2 = gx.FEE_PER_KM;
            a3 = (vala * a2) + (a1 * float.Parse(TextBox16.Text.ToString()));
            TextBox17.Text = a3.ToString();
        }
        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox1.Checked == true)
            {
                Panel1.Visible = false;
                Panel2.Visible = true;
            }
            else
            {
                Panel1.Visible = true;
                Panel2.Visible = false;
            }
        }

        protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (IsValid)
                {
                    if (CheckBox1.Checked == false)
                    {
                        InsertOderA();
                    }
                    else
                    {
                        if (TextBox16.Text == "")
                        {
                            Alert.Show("Please enter in Textbox Distance");
                        }
                        else
                        {
                            if (TextBox17.Text == "")
                            {
                                Alert.Show("Please enter in Textbox Fee Charge");
                            }
                            else
                            {
                                InsertOderB();
                            }
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                Alert.Show(ex.Message);
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            
                cal();
                RequiredFieldValidator18.Visible = true;
                RegularExpressionValidator3.Visible = true;
                RequiredFieldValidator19.Visible = true;
                RegularExpressionValidator4.Visible = true;
                RequiredFieldValidator20.Visible = true;
                RegularExpressionValidator5.Visible = true;
            
           
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            calB();
        }

        protected void TextBox12_TextChanged(object sender, EventArgs e)
        {
            if (int.Parse(TextBox12.Text.ToString()) < int.Parse(TextBox11.Text.ToString()))
            {
                Alert.Show("'Total Paid' greater than or equal to Fee Charge.");
            }
            else
            {
                TextBox13.Text = (int.Parse(TextBox12.Text.ToString()) - int.Parse(TextBox11.Text.ToString())).ToString();
            }
               
        }
    }
}